started: AL13Jul2019
last updated: AL14Jul2019

Summary

Input sequencing data: 1,850 vars x 715 samples ( 519BC = 258UBC + 260CBC and 197NFE)
Input eigenvectors: 3,219 samples = 715 wecare-nfe + 2,504 kgen

Start_section

Sys.time()
## [1] "2019-07-14 21:22:12 BST"
rm(list=ls())
graphics.off()

library(knitr)
## Warning: package 'knitr' was built under R version 3.5.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
base_folder="/Users/alexey/Documents/wecare/ampliseq/v04_ampliseq_nfe/s14_joined_ampliseq_1kg_PCs/s01_common_variants_not_in_LD_141"

opts_knit$set(root.dir = base_folder)

options(stringsAsFactors = F)
options(warnPartialMatchArgs = T, 
        warnPartialMatchAttr = T, 
        warnPartialMatchDollar = T)

#options(error = browser()) # Type Q or c to exit, drop browser level
# https://support.rstudio.com/hc/en-us/articles/200713843?version=1.1.456&mode=desktop
# https://stackoverflow.com/questions/13052522/how-to-leave-the-r-browser-mode-in-the-console-window/13052588 

Read_data

source_folder="/Users/alexey/Documents/wecare/ampliseq/v04_ampliseq_nfe/s12_check_BRCA1_BRCA2_PALB2"
load(paste(source_folder, "s03_exclude_BRCA1_BCRA2_PALB2_carriers.RData", sep="/"))
base_folder="/Users/alexey/Documents/wecare/ampliseq/v04_ampliseq_nfe/s14_joined_ampliseq_1kg_PCs/s01_common_variants_not_in_LD_141"

eigenvectors_file <- paste(base_folder, "ampliseq_1kg_141_3219_100PCs.eigenvec", sep="/")
eigenvectors.df <- read.table(eigenvectors_file, header=T, sep="\t",quote="")

eigenvalues_file <- paste(base_folder, "ampliseq_1kg_141_3219_100PCs.eigenval", sep="/")
eigenvalues.df <- read.table(eigenvalues_file, header=F, sep="\t",quote="")

kg_phenotypes_file <- paste(base_folder, "integrated_call_samples_v3.20130502.ALL.panel", sep="/")
kg_phenotypes.df <- read.table(kg_phenotypes_file, header=T)

rm(source_folder, eigenvectors_file, eigenvalues_file, kg_phenotypes_file, genotypes.mx, variants.df)

Check data

ls()
## [1] "base_folder"      "eigenvalues.df"   "eigenvectors.df"  "kg_phenotypes.df" "phenotypes.df"
dim(eigenvectors.df)
## [1] 3219  102
dim(eigenvalues.df)
## [1] 100   1
dim(kg_phenotypes.df)
## [1] 2504    4
dim(phenotypes.df)
## [1] 715  24
rownames(eigenvectors.df) <- eigenvectors.df$FID
eigenvectors.df <- eigenvectors.df[,-1]

2504+715
## [1] 3219

Look at eigenvalues

plot(eigenvalues.df$V1, type="b", ylab="Variance",
     main="Top 100 eigenvectors")

plot(eigenvalues.df$V1[1:10], type="b", ylab="Variance",
     main="Top 10 eigenvectors")

rm(eigenvalues.df)

Deal with overlaping female NFE

# Make a table with IDs of overlapping NFE
eigenvectors.df[c(3022,3023),c("IID","PC1")]
##                     IID         PC1
## 9_S346_L008 9_S346_L008 -0.01548790
## 2:HG00097     2:HG00097 -0.00760571
nfe_pca <- eigenvectors.df$IID[3023:3219]
nfe_ampliseq <- sub("2:","",nfe_pca)
nfe.df <- data.frame(nfe_ampliseq, nfe_pca)

# Remove overlapping NFE from eigenvectors data
selected_samples <- ! eigenvectors.df$IID %in% nfe.df$nfe_pca
sum(selected_samples)
## [1] 3022
518+2504
## [1] 3022
eigenvectors_ampliseq_kgen.df <- eigenvectors.df[selected_samples,1:6]
"sample" -> colnames(eigenvectors_ampliseq_kgen.df)[1]

# Prepare phenotypes for ampliseq-kgen data
phenotypes.df[c(518:519),c(1,2)]
##                long_ids illumina_id
## 9_S346_L008 9_S346_L008        S346
## HG00097         HG00097        <NA>
phenotypes_ampliseq.df <- phenotypes.df[1:518,c("long_ids","cc")]
table(phenotypes_ampliseq.df$cc)
## 
##   0   1 
## 258 260
"WECARE" -> phenotypes_ampliseq.df$cc[phenotypes_ampliseq.df$cc==1]
"WECARE" -> phenotypes_ampliseq.df$cc[phenotypes_ampliseq.df$cc==0]
table(phenotypes_ampliseq.df$cc)
## 
## WECARE 
##    518
c("sample","group") -> colnames(phenotypes_ampliseq.df)

phenotypes_kgen.df <- kg_phenotypes.df[,c("sample","super_pop")]
c("sample","group") -> colnames(phenotypes_kgen.df)

phenotypes_ampliseq_kgen.df <- rbind(phenotypes_kgen.df,phenotypes_ampliseq.df)
table(phenotypes_ampliseq_kgen.df$group)
## 
##    AFR    AMR    EAS    EUR    SAS WECARE 
##    661    347    504    503    489    518
# Add eigenvectors to phenotypes
eigenphen_ampliseq_kgen.df <- full_join(
  phenotypes_ampliseq_kgen.df, eigenvectors_ampliseq_kgen.df, by="sample")
dim(eigenphen_ampliseq_kgen.df)
## [1] 3022    7
head(eigenphen_ampliseq_kgen.df)
##    sample group         PC1         PC2        PC3         PC4         PC5
## 1 HG00096   EUR -0.01704280 -0.01395700  0.0200111 -0.01959080  0.01154500
## 2 HG00097   EUR -0.00697579 -0.00822148  0.0194563 -0.02681220 -0.00485561
## 3 HG00099   EUR -0.00734417 -0.00324553  0.0350950  0.00172578  0.02210510
## 4 HG00100   EUR -0.00905428 -0.02275700  0.0199476  0.00914868  0.00392881
## 5 HG00101   EUR -0.01632370 -0.00714502  0.0158315  0.00904719  0.00314675
## 6 HG00102   EUR -0.00791246 -0.00488407 -0.0099180 -0.02362460 -0.02879070
tail(eigenphen_ampliseq_kgen.df)
##            sample  group        PC1         PC2         PC3         PC4         PC5
## 3017 95_S517_L008 WECARE -0.0136326  0.00946938  0.00760401 -0.00952336 -0.00110847
## 3018 96_S236_L007 WECARE -0.0126025 -0.01037420  0.02435530 -0.00122289  0.00621415
## 3019 97_S509_L008 WECARE -0.0132320 -0.01458540  0.03697160 -0.00174267 -0.00345524
## 3020 98_S335_L008 WECARE -0.0132499 -0.00650475  0.00746894 -0.02366860 -0.00698531
## 3021 99_S418_L008 WECARE -0.0136892 -0.00888814  0.01321000  0.00408557 -0.00173579
## 3022  9_S346_L008 WECARE -0.0154879 -0.00553968 -0.00856120 -0.02921850 -0.00299197
# Clean-up
rm(nfe_pca, nfe_ampliseq, selected_samples, eigenvectors_ampliseq_kgen.df, phenotypes_ampliseq.df,
   phenotypes_ampliseq_kgen.df)

Plot eigenvectors

# Prepare vector fr colour scale
myColours <- c("EUR"="BLUE", "AFR"="YELLOW", "AMR"="GREEN",
               "SAS"="GREY", "EAS"="PINK", 
               "WECARE"="RED")

myColourScale <- scale_colour_manual(values=myColours)

# Static plot
ggplot(eigenphen_ampliseq_kgen.df, aes(PC1,PC2)) + 
  geom_point(aes(col=group)) +
  labs(title="141 common variants not in LD", x="PC1", y="PC2") +
  myColourScale

# Interactive plot
plotly_group <- factor(eigenphen_ampliseq_kgen.df$group,
  levels=c("AFR","AMR","EAS","SAS","EUR","WECARE"))

g <- ggplot(eigenphen_ampliseq_kgen.df, aes(PC1,PC2)) + 
  geom_point(aes(col=plotly_group, text=sample)) +
  labs(title="141 common variants not in LD", x="PC1", y="PC2") +
  theme(legend.title=element_blank()) + # To suppress the legend title
  myColourScale                         # otherwise it would be "plotly_group"
## Warning: Ignoring unknown aesthetics: text
ggplotly(g, tooltip="text") # By default the tooltip would also show coordinates 
## Warning in dev_fun(file = tempfile(), width = width %||% 640, height = height %||% : partial argument match of 'file' to 'filename'
# Clean-up
rm(myColours, myColourScale, g)

Final_section

ls()
## [1] "base_folder"                "eigenphen_ampliseq_kgen.df" "eigenvectors.df"            "kg_phenotypes.df"           "nfe.df"                     "phenotypes_kgen.df"         "phenotypes.df"              "plotly_group"
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS  10.14.5
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] plotly_4.9.0  ggplot2_3.2.0 dplyr_0.8.1   knitr_1.23   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.1        later_0.8.0       pillar_1.4.1      compiler_3.5.1    tools_3.5.1       digest_0.6.19     jsonlite_1.6      evaluate_0.14     tibble_2.1.3      gtable_0.3.0      viridisLite_0.3.0 pkgconfig_2.0.2   rlang_0.3.4       shiny_1.3.2       crosstalk_1.0.0   yaml_2.2.0        xfun_0.7          withr_2.1.2       stringr_1.4.0     httr_1.4.0        htmlwidgets_1.3   grid_3.5.1        tidyselect_0.2.5  glue_1.3.1        data.table_1.12.2 R6_2.4.0          rmarkdown_1.13    purrr_0.3.2       tidyr_0.8.3       magrittr_1.5      promises_1.0.1    scales_1.0.0      htmltools_0.3.6   assertthat_0.2.1  xtable_1.8-4      mime_0.7          colorspace_1.4-1  httpuv_1.5.1      labeling_0.3      stringi_1.4.3     lazyeval_0.2.2    munsell_0.5.0     crayon_1.3.4
Sys.time()
## [1] "2019-07-14 21:22:15 BST"